;; **************************************************************************
;; * Symbolics Lisp Machine Keyboard to PS/2 Converter w/ Programmable Keys *
;; *                Copyright (c)2008 Stanislav Datskovskiy                 *
;; *                          stas@loper-os.org                             *
;; *                                                                        *
;; *                    See README for electric schematics                  *
;; *                                                                        *
;; *  This program is free software; you can redistribute it and/or modify  *
;; *  it under the terms of the GNU General Public License as published by  *
;; *  the Free Software Foundation; either version 3 of the License, or     *
;; *  (at your option) any later version.                                   *
;; *                                                                        *
;; *  This program is distributed in the hope that it will be useful,       *
;; *  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
;; *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
;; *  GNU General Public License for more details.                          *
;; *                                                                        *
;; *  You should have received a copy of the GNU General Public License     *
;; *  along with this program.  If not, see <http://www.gnu.org/licenses/>. *
;; **************************************************************************


#include	"common.asm"

; Delay = 1 seconds
; Clock frequency = 4 MHz

; Actual delay = 1 seconds = 1000000 cycles
; Error = 0 %


;	variable d1, d2, d3

    udata

d1	res	1
d2	res	1
d3	res	1
SDL	res	1
del_A	res	1
del_B	res	1
del_C	res	1


	code

Delay_second
                        ;999997 cycles
        movlw   0x08
        movwf   d1
        movlw   0x2F
        movwf   d2
        movlw   0x03
        movwf   d3
Delay_0
        decfsz  d1, f
        goto    $+2
        decfsz  d2, f
        goto    $+2
        decfsz  d3, f
        goto    Delay_0
                        ;3 cycles
        goto    $+1
        nop
        return



; Delay = 0.5 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.5 seconds = 500000 cycles
; Error = 0 %

Delay_half_second
			;499994 cycles
	movlw	0x03
	movwf	d1
	movlw	0x18
	movwf	d2
	movlw	0x02
	movwf	d3
Delay_1
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_1

			;6 cycles
	goto	$+1
	goto	$+1
	goto	$+1
	return



                ; ************************ Delay_Ms ***********************
delay_ms        ; delay ~SDL ms at 4MHz clk ( = 1Mhz inst.); 1ms = 1000 cyc.
                movf    SDL,w
                xorlw   D'1'
                skpnz
                goto    shortdel
                movf    SDL,w
                movwf   del_C
del_loop
                call    del_1ms
                decfsz  del_C,f
                goto    del_loop
                return
shortdel
                movlw   D'255'
                movwf   del_C
swait
                decfsz  del_C,f
                goto    swait
                return
                ; ************************ Delay_1Ms **********************
del_1ms
                ;981 cycles
                movlw   0x0e
                movwf   del_A
Delay_00
                movlw   0x16
                movwf   del_B
Delay_01
                decfsz  del_B, f
                goto    Delay_01
                decfsz  del_A, f
                goto    Delay_00
                ;13 cycles
                movlw   0x04
                movwf   del_A
Delay_10
                decfsz  del_A, f
                goto    Delay_10
                ;2 cycles
                goto    $+1
                ;4 cycles (including call)
                return
                ; *********************************************************

    global	SDL
    global	Delay_second
    global	delay_ms
        
end